#include <SoftwareSerial.h>

#define rxPin 7
#define txPin 6

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte incomingByte;	// for incoming serial data
int serIn;		 // var that will hold the bytes-in read from the serialBuffer
char serInString[12]; // array that will hold the different bytes  100=100characters;
			     // -> you must state how long the array will be else it won't work.
int serInIndx  = 0;    // index of serInString[] in which to inser the next incoming byte
int serOutIndx = 0;    // index of the outgoing serInString[] array;


void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
  Serial.begin(19200);	// opens serial port, sets data rate to 19200 bps
}


void loop () {
  //simple feedback from Arduino
  mySerial.write(0xfd);
  mySerial.write(0xfa);
  // only if there are bytes in the serial buffer execute the following code
  if(mySerial.available()) {    
    
    //keep reading from serial untill there are no bytes in the serial buffer
     while (mySerial.available()){	  
	  serIn = mySerial.read();		  //read Serial	  
	  serInString[serInIndx] = serIn; //insert the byte you just read into the array at the specified index
	  serInIndx++;			  //update the new index
     }
    
     //feedback that something was received
     Serial.println ("Processing Tag");
  }
  
  //do somenthing else perhaps wait for other data.
  //Serial.println ("NO TAG ");
  
  //print out later in the loop the sentence only if it has actually been collected;
  if( serInIndx > 0) {
	Serial.print("Tag ID= ");

	//loop through all bytes in the array and print them out
	for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) {
	    Serial.print( (serInString[serOutIndx]), HEX );    //print out the byte at the specified index
	    //serInString[serOutIndx] = "";		//optional: flush out the content
	}
	  
	//reset all the functions to be able to fill the string back with content
	serOutIndx = 0;
	serInIndx = 0;
  }
    
  //slows down the visualization in the terminal
  Serial.println();
  delay(200);
}
